﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Media;
using System.Xml.Serialization;

namespace LevelEditor.Model.Entity
{
    [Serializable]
    public class Scene : INotifyPropertyChanged
    {
        public Scene() {
            this.PhysicConfig = new PhysicConfig();
        }

        // Fields
        private Color backgroundColor;
        private int gridSquareSize;
        private double height;
        private double width;

        // Events
        public event PropertyChangedEventHandler PropertyChanged;

        // Methods
        public void ArrangeZOrder()
        {
            int z = 0;
            for (int i = this.Layers.Count - 1; i >= 0; i--)
            {
                for (int j = this.Layers[i].GameSprites.Count - 1; j >= 0; j--)
                {
                    this.Layers[i].GameSprites[j].ZIndex = z;
                    z++;
                }
            }
        }

        private void NotifyPropertyChanged(string propertyName)
        {
            if (this.PropertyChanged != null)
            {
                this.PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
            }
        }

        public void RemoveGameSprite(GameSprite gameSprite)
        {
            for (int i = this.Layers.Count - 1; i >= 0; i--)
            {
                if (this.Layers[i].GameSprites.Contains(gameSprite))
                {
                    this.Layers[i].GameSprites.Remove(gameSprite);
                }
            }
        }

        // Properties
        [Category("Appearance"), DisplayName("BackgroundColor")]
        public Color BackgroundColor
        {
            get
            {
                return this.backgroundColor;
            }
            set
            {
                this.backgroundColor = value;
                this.NotifyPropertyChanged("BackgroundColor");
            }
        }

        [DisplayName("GridSquareSize"), Category("LayoutOptions")]
        public int GridSquareSize
        {
            get
            {
                return this.gridSquareSize;
            }
            set
            {
                this.gridSquareSize = value;
                this.NotifyPropertyChanged("GridSquareSize");
            }
        }

        [DisplayName("Height"), Category("Appearance")]
        public double Height
        {
            get
            {
                return this.height;
            }
            set
            {
                this.height = value;
                this.NotifyPropertyChanged("Height");
            }
        }

        [XmlArray]
        public List<Layer> Layers { get; set; }

        public int NewSpriteId { get; set; }

        [DisplayName("Width"), Category("Appearance")]
        public double Width
        {
            get
            {
                return this.width;
            }
            set
            {
                this.width = value;
                this.NotifyPropertyChanged("Width");
            }
        }
        [DisplayName("PhysicConfig"), Category("Physics")]
        public PhysicConfig PhysicConfig { get; set; }
    }


}
